/**
 * Axios 配置文件
 * 简化的环境配置
 */
import type { AxiosRequestConfig } from 'axios'

/** 后端类型 */
export type BackendType = 'mock' | 'java' | 'python' | 'node'

/** 环境类型 */
export type Environment = 'development' | 'testing' | 'staging' | 'production'

/** Axios 配置接口 */
export interface ApiConfig extends AxiosRequestConfig {
  name: string
  baseURL: string
}

/** 获取当前环境 */
export const getCurrentEnvironment = (): Environment => {
  const urlParams = new URLSearchParams(window.location.search)
  const urlEnv = urlParams.get('env') as Environment
  const packageEnv = import.meta.env.MODE as Environment

  return urlEnv || packageEnv
}

/** API 类型 */
export type ApiType = 'article'

/** 环境下各 API 类型的后端映射 */
const envApiBackends: Record<Environment, Record<ApiType, BackendType>> = {
  development: {
    article: 'mock',
  },
  testing: {
    article: 'java',
  },
  staging: {
    article: 'node',
  },
  production: {
    article: 'node',
  },
}

/** 获取指定 API 类型的后端 */
export const getApiBackend = (env: Environment, apiType: ApiType): BackendType => {
  const urlParams = new URLSearchParams(window.location.search)
  const urlBackend = urlParams.get(`${apiType}_backend`) as BackendType
  const savedBackend = localStorage.getItem(`${apiType}_backend`) as BackendType

  return urlBackend || savedBackend || envApiBackends[env][apiType]
}

/** 获取当前环境下各 API 的后端配置 */
export const getCurrentApiBackends = (env: Environment) => {
  const result: Record<ApiType, BackendType> = {} as Record<ApiType, BackendType>

  Object.keys(envApiBackends[env]).forEach((apiType) => {
    result[apiType as ApiType] = getApiBackend(env, apiType as ApiType)
  })

  return result
}

/** 多环境配置对象 */
export const config = {
  development: {
    mock: {
      name: 'Mock 服务',
      baseURL: 'http://localhost:3002/api',
      timeout: 5000,
      headers: {
        'Content-Type': 'application/json',
      },
    },
    node: {
      name: 'Node.js 后端',
      baseURL: 'http://localhost:3002/api',
      timeout: 10000,
      headers: {
        'Content-Type': 'application/json',
        'API-Version': 'v1',
      },
    },
    java: {
      name: 'Java 后端',
      baseURL: 'http://localhost:8080/api',
      timeout: 15000,
      headers: {
        'Content-Type': 'application/json',
      },
    },
    python: {
      name: 'Python 后端',
      baseURL: 'http://localhost:8000/api',
      timeout: 12000,
      headers: {
        'Content-Type': 'application/json',
      },
    },
  },
  testing: {
    mock: {
      name: 'Mock 服务',
      baseURL: 'http://localhost:3001/api',
      timeout: 5000,
      headers: {
        'Content-Type': 'application/json',
      },
    },
    node: {
      name: 'Node.js 后端',
      baseURL: 'http://localhost:3002/api',
      timeout: 10000,
      headers: {
        'Content-Type': 'application/json',
        'API-Version': 'v1',
      },
    },
    java: {
      name: 'Java 后端',
      baseURL: 'http://localhost:8080/api',
      timeout: 15000,
      headers: {
        'Content-Type': 'application/json',
      },
    },
    python: {
      name: 'Python 后端',
      baseURL: 'http://localhost:8000/api',
      timeout: 12000,
      headers: {
        'Content-Type': 'application/json',
      },
    },
  },
  staging: {
    mock: {
      name: 'Mock 服务',
      baseURL: 'https://mock-staging.zblog.com/api',
      timeout: 8000,
      headers: {
        'Content-Type': 'application/json',
      },
    },
    node: {
      name: 'Node.js 后端',
      baseURL: 'https://api-staging.zblog.com/api',
      timeout: 15000,
      headers: {
        'Content-Type': 'application/json',
        'API-Version': 'v1',
      },
    },
    java: {
      name: 'Java 后端',
      baseURL: 'https://java-staging.zblog.com/api',
      timeout: 20000,
      headers: {
        'Content-Type': 'application/json',
      },
    },
    python: {
      name: 'Python 后端',
      baseURL: 'https://python-staging.zblog.com/api',
      timeout: 18000,
      headers: {
        'Content-Type': 'application/json',
      },
    },
  },
  production: {
    mock: {
      name: 'Mock 服务',
      baseURL: 'https://mock.zblog.com/api',
      timeout: 8000,
      headers: {
        'Content-Type': 'application/json',
      },
    },
    node: {
      name: 'Node.js 后端',
      baseURL: 'https://api.zblog.com/api',
      timeout: 15000,
      headers: {
        'Content-Type': 'application/json',
        'API-Version': 'v1',
      },
    },
    java: {
      name: 'Java 后端',
      baseURL: 'https://java.zblog.com/api',
      timeout: 20000,
      headers: {
        'Content-Type': 'application/json',
      },
    },
    python: {
      name: 'Python 后端',
      baseURL: 'https://python.zblog.com/api',
      timeout: 18000,
      headers: {
        'Content-Type': 'application/json',
      },
    },
  },
} as const

/** 当前环境和 API 后端映射 */
export const CURRENT_ENV = getCurrentEnvironment()
export const CURRENT_API_BACKENDS = getCurrentApiBackends(CURRENT_ENV)

// 美化的配置信息打印
console.group('🚀 ZBlog API 配置')
console.log(
  `📍 当前环境: %c${CURRENT_ENV}`,
  'color: #2563eb; font-weight: bold; background: #eff6ff; padding: 2px 6px; border-radius: 4px;',
)

console.group('🔗 API 路由映射')
Object.entries(CURRENT_API_BACKENDS).forEach(([api, backend]) => {
  const backendConfig = config[CURRENT_ENV][backend]
  console.log(
    `  %c${api}%c → %c${backend}%c (%c${backendConfig.baseURL}%c)`,
    'color: #7c3aed; font-weight: bold;',
    'color: #6b7280;',
    'color: #059669; font-weight: bold; background: #ecfdf5; padding: 1px 4px; border-radius: 3px;',
    'color: #6b7280;',
    'color: #0891b2; font-style: italic;',
    'color: inherit;',
  )
})
console.groupEnd()
console.groupEnd()
